package com.example.win.android_banduyujia.peopleactivity.fragment.projectfragment.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.win.android_banduyujia.R;
import com.example.win.android_banduyujia.login.application.LoginApplication;
import com.example.win.android_banduyujia.peopleactivity.fragment.projectfragment.adapter.FragmentAdapter;
import com.example.win.android_banduyujia.entity.MyProjectEntity;
import com.example.win.android_banduyujia.peopleactivity.fragment.projectfragment.database.MyCourseDatabase;
import com.example.win.android_banduyujia.peopleactivity.ui.layout.RefreshLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by huchun on 2015/8/21.
 */
public class WaitFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener, RefreshLayout.OnLoadListener {
    private static final String TAG = WaitFragment.class.getSimpleName();

    private RefreshLayout     swipeLayout;
    private List<MyProjectEntity>     mDataList;
    private ArrayList<HashMap<String, String>> list;
    private View        header;
    private Context     context;

    private RecyclerView      recyclerView;
    private MyCourseDatabase  mDatabase;
    private FragmentAdapter   mAdapter;

    private int  mCurrentPage;
    private int  mLastPage;
    private String  mAccressToken;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View  view = inflater.inflate(R.layout.fragment_wait_page, null);

        mAccressToken = LoginApplication.getInstance().getAccessToken();
        mDatabase = MyCourseDatabase.getInstance();

        InitViews(view);
        loadDatas();

        header =getLayoutInflater(savedInstanceState).inflate(R.layout.layout_refresh_header, null);
        return view;
    }

    private void InitViews(View view) {
        swipeLayout= (RefreshLayout)view.findViewById(R.id.swipe_container);
        swipeLayout.setOnRefreshListener(this);
        swipeLayout.setColorScheme(android.R.color.holo_blue_bright,
                android.R.color.holo_green_light,
                android.R.color.holo_orange_light,
                android.R.color.holo_red_light);

        list = new ArrayList<HashMap<String,String>>();
        for(int i=0; i <10;i++){
            list.add(new HashMap<String, String>());
        }

        recyclerView = (RecyclerView) view.findViewById(R.id.revyvler_view_vertical);

        LinearLayoutManager  layoutManager = new LinearLayoutManager(context);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);

        String[]  dataset = new String[2];
        for (int i=0; i<dataset.length; i++){
            dataset[i] = "item" + i;
        }
        mAdapter = new FragmentAdapter(getActivity(), mDataList);
        recyclerView.setAdapter(mAdapter);
        mAdapter.setListDatas(mDataList);
    }

    private void loadDatas() {
       mDataList = mDatabase.getAllProject();
       if(mDataList == null || mDataList.size() == 0){
           swipeLayout.setVisibility(View.VISIBLE);
       }else{
           swipeLayout.setVisibility(View.GONE);
           mAdapter = new FragmentAdapter(getActivity(), mDataList);
           recyclerView.setOnScrollListener(mAdapter);
           recyclerView.setAdapter(mAdapter);
       }
        refreshDatasFromServer(1);
    }

    @Override
    public void onRefresh() {
        swipeLayout.postDelayed(new Runnable() {
            @Override
            public void run() {
                refreshDatasFromServer(1);
                swipeLayout.setRefreshing(false);

                if(mAdapter == null){
                    mAdapter = new FragmentAdapter(getActivity(), mDataList);
                    recyclerView.setAdapter(mAdapter);
                    recyclerView.setOnScrollListener(mAdapter);
                    swipeLayout.setOnRefreshListener((SwipeRefreshLayout.OnRefreshListener) mAdapter);
                }else{
                    mAdapter.setListDatas(mDataList);
                }
            }
        },1000);
    }

    @Override
    public void onLoad() {
        swipeLayout.postDelayed(new Runnable() {
            @Override
            public void run() {
               // swipeLayout.setLoading(false);
                if(mCurrentPage != mLastPage){
                    refreshDatasFromServer(mCurrentPage+1);
                }
                if(mAdapter == null){
                    mAdapter = new FragmentAdapter(getActivity(), mDataList);
                    recyclerView.setAdapter(mAdapter);
                    recyclerView.setOnScrollListener(mAdapter);
                    swipeLayout.setOnLoadListener((RefreshLayout.OnLoadListener) mAdapter);
                }else{
                    mAdapter.setListDatas(mDataList);
                }
            }
        },1000);
    }

    private void refreshDatasFromServer(int page) {

    }
}
